//
//  WeDetailController.m
//  WeGalaxy
//
//  Created by Zheng on 2017/6/27.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeDetailController.h"

#import "WeDetailUserInfoCell.h"
#import "WeDetailContentInfoCell.h"
#import "WeDetailBottomView.h"
#import "WeDetailLeaveMessageTitleView.h"
#import "WeDetailLeaveMessageHeaderView.h"
#import "WeDetailLeaveMessageCell.h"


static NSString *const kWeDetailUserInfoCell_ID = @"kWeDetailUserInfoCell_ID";
static NSString *const kWeDetailContentInfoCell_ID = @"kWeDetailContentInfoCell_ID";
static NSString *const kWeDetailLeaveMessageCell_ID = @"kWeDetailLeaveMessageCell_ID";

@interface WeDetailController () {
    CGFloat _rowHeight; //图片自适应单元格高度
}

@property (nonatomic, strong) UIButton *saveBtn; //收藏
@property (nonatomic, strong) UIButton *shareBtn; //分享
@property (nonatomic, strong) UILabel *timeLbl; //倒计时
@property (nonatomic, strong) WeDetailBottomView *bottomView; //底部功能视图
@property (nonatomic, strong) UILabel *leaveTitleLbl; //留言标题
@property (nonatomic, copy) NSArray *msgDataArr;


@end

@implementation WeDetailController


#pragma mark - tableview datasource&&delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 2) {
        return 1;
    }
    
    else if (section == 3) {
        return 2;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WeDetailUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeDetailUserInfoCell_ID forIndexPath:indexPath];
        return cell;
    } else if (indexPath.section == 1) {
        WeDetailContentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeDetailContentInfoCell_ID forIndexPath:indexPath];
        
        NSArray *images = @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498550246964&di=f006ae0c0881c78dc4d10104f9c6211f&imgtype=0&src=http%3A%2F%2Fimg.game333.net%2Fuploads%2Fshouyou%2F2017%2F05%2F26%2F2017052625734222.jpg",
                            @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498551591984&di=460028c0b852a161e8ab1160b66cbb76&imgtype=0&src=http%3A%2F%2Fnewsimg.5054399.com%2Fuploads%2Fuserup%2F1702%2F13155JQ347.jpg",
                            @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498551591982&di=f068bdc2a914e8fec75309f62c30ac5f&imgtype=0&src=http%3A%2F%2Fimg1.gamersky.com%2Fimage2016%2F12%2F20161209_gxh_289_1%2Fimage029_S.jpg",
                            @"http://web.52pk.com/uploads/170227/8_171115_1_lit.jpg"];
        
        cell.images = images;
        
        cell.totalHeightBlock = ^(CGFloat totalHeight) {
            _rowHeight = totalHeight;
            [self.myTableView reloadData];
        };
        
        return cell;
    } else {
        WeDetailLeaveMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:kWeDetailLeaveMessageCell_ID forIndexPath:indexPath];
        return cell;
    }

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 44;
    
    if (indexPath.section == 0) {
        height = 44;
    } else if (indexPath.section == 1) {
        height = _rowHeight;
    } else {
        height = 75;
    }
    
    return height;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        WeDetailLeaveMessageTitleView *lmtView = [[WeDetailLeaveMessageTitleView alloc] init];
        return lmtView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 44;
    }
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return nil;
    } else {
        WeDetailLeaveMessageHeaderView *headerView = [[WeDetailLeaveMessageHeaderView alloc] init];
        return headerView;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 0.001;
    } else {
        return 75;
    }
}

#pragma mark - 点击事件
- (void)buttonClickAction:(UIButton *)sender {
    if (sender == _saveBtn) {
        kCX_LOG(@"收藏");
    } else if (sender == _shareBtn) {
        kCX_LOG(@"分享");
    }    
}

- (void)buttonClickActionWithStyle:(WeDetailBottomViewClickStyle)style {
    switch (style) {
        case WeDetailBottomViewClickStyleLeaveMessage:
            kCX_LOG(@"留言");
            break;
        case WeDetailBottomViewClickStyleThumbUp:
            kCX_LOG(@"点赞");
            break;
        case WeDetailBottomViewClickStylePrice:
            kCX_LOG(@"聊一聊");
            break;
        case WeDetailBottomViewClickStyleChat:
            kCX_LOG(@"出价");
            break;
    }
}

#pragma mark - 子视图
- (void)subviews {
    
    [self.view addSubview:self.bottomView];
    
    [self.navigationController.navigationBar addSubview:self.saveBtn];
    [self.navigationController.navigationBar addSubview:self.shareBtn];
    
    [self.myTableView registerClass:[WeDetailUserInfoCell class] forCellReuseIdentifier:kWeDetailUserInfoCell_ID];
    [self.myTableView registerClass:[WeDetailContentInfoCell class] forCellReuseIdentifier:kWeDetailContentInfoCell_ID];
    [self.myTableView registerClass:[WeDetailLeaveMessageCell class] forCellReuseIdentifier:kWeDetailLeaveMessageCell_ID];
    
    self.myTableView.tableHeaderView = self.timeLbl;
    
    CGFloat myTableHeight = kSCREENH_HEIGHT-kTAB_BAR_Height-kNAV_BAR_Height-kSTATUS_BAR_Height;
    self.myTableView.frame = (CGRect){0,0,kSCREEN_WIDTH,myTableHeight};
    
    [self layoutSubviews];
}

- (void)layoutSubviews {
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.height.equalTo(@(kTAB_BAR_Height));
    }];
}

- (WeDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[WeDetailBottomView alloc] init];
        __weak typeof(self) weakSelf = self;
        _bottomView.clickActionBlock = ^(WeDetailBottomViewClickStyle style) {
            [weakSelf buttonClickActionWithStyle:style];
        };
    }
    return _bottomView;
}

- (UILabel *)timeLbl {
    if (!_timeLbl) {
        _timeLbl = [[UILabel alloc] init];
        _timeLbl.frame = (CGRect){0,0,kSCREEN_WIDTH,20};
        _timeLbl.backgroundColor = kRGB_COLOR(244, 66, 54, 1);
        _timeLbl.textColor = [UIColor whiteColor];
        _timeLbl.textAlignment = NSTextAlignmentCenter;
        _timeLbl.font = kCX_FONT(13);
    }
    return _timeLbl;
}

- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [self getClickButton:(CGRect){kSCREEN_WIDTH-kNAV_BAR_Height,0,kNAV_BAR_Height,kNAV_BAR_Height}];
    }
    return _saveBtn;
}

- (UIButton *)shareBtn {
    if (!_shareBtn) {
        _shareBtn = [self getClickButton:(CGRect){kSCREEN_WIDTH-kNAV_BAR_Height*2,0,kNAV_BAR_Height,kNAV_BAR_Height}];
    }
    return _shareBtn;
}

- (UIButton *)getClickButton:(CGRect)frame {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = kRGB_COLOR(arc4random()%255, arc4random()%255, arc4random()%255, 1);
    return btn;
}

#pragma mark - 视图生命周期
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _shareBtn.hidden = YES;
    _saveBtn.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _shareBtn.hidden = NO;
    _saveBtn.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"详情";
    
    _rowHeight = 170;
    
    [self subviews];
    
    [self timeCountDownWithSecond:1800];
    
    
}

#pragma mark - 倒计时
- (void)timeCountDownWithSecond:(NSInteger)time {
    __block NSInteger timeout = time;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        if(timeout < 0){
            dispatch_source_cancel(timer);
        } else {
            NSInteger minute = timeout/60;
            NSString *minuteStr = @"";
            minuteStr = minute >= 10 ? [NSString stringWithFormat:@"%ld",minute]: [NSString stringWithFormat:@"0%ld",minute];
            
            NSString *secondStr = @"";
            NSInteger second = timeout%60;
            secondStr = second >= 10 ? [NSString stringWithFormat:@"%ld",second]: [NSString stringWithFormat:@"0%ld",second];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                _timeLbl.text = [NSString stringWithFormat:@"距离拍卖结束时间 00:%@:%@",minuteStr,secondStr];
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
